#include "KeButtonCombox.h"

#include <QPainter>
#include <QPen>

#include "../../uitools/IdRootMenu.h"
#include "../../uitools/vhc/constview4fun.h"
#include "../../uitools/vhc/constview4id.h"

#include "../../uitools/TViewDraw.h"

#include "../MacroFunOfView.h"




KeButtonCombox::KeButtonCombox(QWidget *parent) :
    QWidget{parent},
    isWindowDeactivate(false),
    m_nTextCode(0),
    m_nIdTouchedDown(0),
    m_nMouseFlag(0)
    ,m_nMousePosition(Pos_ComboBox_None)

{
    QSizePolicy sizePoFixed(QSizePolicy::Policy::Fixed, QSizePolicy::Policy::Fixed);
    sizePoFixed.setHorizontalStretch(0);
    sizePoFixed.setVerticalStretch(0);
    sizePoFixed.setHeightForWidth(false);

    setSizePolicy(sizePoFixed);
    setFixedSize(Width_ComboBox,Height_CELL);

    setMouseTracking(true);
    QObject::installEventFilter(this);
}

KeButtonCombox::~KeButtonCombox()
{

}

bool KeButtonCombox::eventFilter(QObject *watched, QEvent *event)
{
    WindowDeactivate_EventFilter_Only()
}

void KeButtonCombox::setTextCode(quint32 code)
{
    if(code != this->m_nTextCode)
    {
        bool inited = 0 == this->m_nTextCode;
        this->m_nTextCode = code;
        if(false == inited)
        {
            repaint();
        }
    }

}

quint32 KeButtonCombox::getTextCode() const
{
    return m_nTextCode;
}

void KeButtonCombox::setIconFont(const QFont &f)
{
    fontIcon = f;
    fontIcon.setPointSize(TextSize_IconFont);
}

int KeButtonCombox::getPos4Combox(const QPointF &point)
{
    int x0 = qRound(point.x());
    int w = this->width();
    int dd = w - RightClick_ComboBox;
    int comboBoxFlag;
    if(x0<1||x0>w)
    {
        comboBoxFlag = Pos_ComboBox_None;
    }else{
        if(x0 < dd)
        {
            comboBoxFlag = Pos_ComboBox_Left;
        }
        else
        {
            comboBoxFlag = Pos_ComboBox_Right;
        }
    }
    return comboBoxFlag;
}



void KeButtonCombox::leaveEvent(QEvent *e)
{

    m_nMouseFlag = 0;
    m_nMousePosition = Pos_ComboBox_None;
    QWidget::leaveEvent(e);
    repaint();
}

void KeButtonCombox::mouseMoveEvent(QMouseEvent *event)
{

    QPointF point = event->position();
    int state = getPos4Combox(point);
    bool ischange =  m_nMousePosition==state;

    m_nMousePosition = state;
    m_nMouseFlag = state;

    // QWidget::mouseMoveEvent(event);
    if(!ischange)
        repaint();
}


void KeButtonCombox::mousePressEvent(QMouseEvent *event)
{
    Qt::MouseButton btn = event->button();
    if(Qt::MouseButton::LeftButton == btn)
    {
        QPointF point = event->position();



        int state = getPos4Combox(point);
        m_nIdTouchedDown = state;
        m_nMouseFlag = BoolFlagMouseDown | state;

        ptDown = point;

        QWidget::mouseMoveEvent(event);

        repaint();

    }
}
void KeButtonCombox::mouseReleaseEvent(QMouseEvent *event)
{
    int idTouched = m_nIdTouchedDown;

    m_nIdTouchedDown = 0;

    if((idTouched))
    {
        QPointF pointUp = event->position();
        QPointF  ptUp = pointUp - ptDown;
        int x = ptUp.x(),y = ptUp.y();
        bool isClicked = x >-DisTorClicked && x < DisTorClicked && y >-DisTorClicked && y < DisTorClicked;
        if(isClicked)
        {
            QWidget::mouseReleaseEvent(event);
            Q_EMIT onClickedMComboxSignals(m_nTextCode,idTouched);

            return;
        }

    }

    QWidget::mouseReleaseEvent(event);
}

void KeButtonCombox::paintEvent(QPaintEvent *event)
{
    QWidget::paintEvent(event);

    QPainter painter(this);

    bool isMouseOver = BoolFlagMouseOver ==(BoolFlagMouseOver & m_nMouseFlag);
    bool isDeAct_noOver = false == isMouseOver && isWindowDeactivate;
    bool isDown = isWindowDeactivate &&(m_nIdTouchedDown);
    int cx0, cy0,ntLeft,ntTop,w,h,boolFlag=0;
    int textSize = 12;
    quint32 textColor = (isDeAct_noOver)? Color_Text_Deactivate:Color_Font_ToolBar;


    w = this->width();
    h = this->height();
    if(isDown)
        textColor = Color_Text_BtnDown;




    if(isDeAct_noOver)boolFlag |= BoolFlagDeactivate;
    if(isDown)boolFlag |= BoolFlagMouseDown;






    if(isMouseOver)
    {
        if(false == isWindowDeactivate)
        {
            bool isOnRight = Pos_ComboBox_Right ==(Pos_ComboBox_Right & m_nMouseFlag);
            QBrush brushBtnOver(QColor(QRgba64::fromArgb32(Background_BtnOver)),Qt::SolidPattern);
            if(isOnRight){
                int w = this->width();
                int dd = w - RightClick_ComboBox;
                QRect rectBound(dd, 0, RightClick_ComboBox,h);
                painter.fillRect(rectBound,brushBtnOver);
            }else
            {
                QRect rectBound(0, 0, w,h);
                painter.fillRect(rectBound,brushBtnOver);
            }


        }else
        {
            // true === isWindowDeactivate
            QRect rectBound(0, 0, w,h);
            painter.fillRect(rectBound,Qt::GlobalColor::white);
        }
    }





    ntLeft = w - Width_ComboBoxFlag ;
    ntTop = h - Height_ComboBoxFlag-4;
    cx0 = ntLeft + (Width_ComboBoxFlag >> 1);
    cy0 = ntTop + (Height_ComboBoxFlag >> 1);
    kepen::core::TViewDraw::DrawComboxFlag(&painter,cx0,cy0,boolFlag);


    QFont &font_t = fontIcon;

    QChar chTxt(m_nTextCode);
    QString szLabel(chTxt);
    font_t.setPointSize(textSize);
    cx0 =  (Width_CELL >> 1);
    cy0 =   (Height_CELL >> 1);
    ntLeft = cx0 - 1000;
    ntTop = cy0 - 500;
    QRect rect4Text(ntLeft, ntTop, 2000, 1000);
    QBrush brushText(QColor(QRgba64::fromArgb32(textColor)),Qt::SolidPattern);
    QPen penText(brushText,1, Qt::SolidLine);
    painter.setFont(font_t);
    painter.setBrush(Qt::BrushStyle::NoBrush);
    painter.setPen(penText);
    painter.drawText(rect4Text,Qt::AlignCenter,szLabel);

}



